package tw.com.hitevision.whiteboard.android.render;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.core.content.ContextCompat;
import tw.com.hitevision.whiteboard.android.R;
import tw.com.hitevision.whiteboard.android.Whiteboard;
import tw.com.hitevision.whiteboard.android.display.DisplayDpiHelper;


public class EraserAreaView extends View {
    private static final String TAG = EraserAreaView.class.getSimpleName();
    private float radius;
    //    private Paint paint;
//    private int strokeColor;
//    private int fillColor;
//    private Path path;
//    private DashPathEffect dashPathEffect;
    private final int verticalOffset =  getResources().getDimensionPixelSize(R.dimen.eraser_vertical_offset);
    private final int horizontalOffset=  getResources().getDimensionPixelSize(R.dimen.eraser_horizontal_offset);
    private Whiteboard.Input eraserInput = Whiteboard.Input.OBJECT_ERASER;
    private Context context;

    private Paint eraserCircleDashLinePaint;

    public EraserAreaView(Context context) {
        super(context);
        init(context);
    }

    public EraserAreaView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        init(context);
    }

    public EraserAreaView(Context context, AttributeSet attributeSet, int dex) {
        super(context, attributeSet, dex);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        radius = 0;
//        paint = new Paint();
//        strokeColor = ContextCompat.getColor(context, R.color.eraserStroke);
//        fillColor = ContextCompat.getColor(context, R.color.eraserFill);
//        dashPathEffect = new DashPathEffect(new float[] {8.0f, 8.0f}, 0);
//        path = new Path();
//        path.addCircle(verticalOffset, horizontalOffset, radius, Path.Direction.CCW);

        eraserCircleDashLinePaint = new Paint();
        eraserCircleDashLinePaint.setColor(context.getResources().getColor(R.color.eraser_oem_dashline_color));
        eraserCircleDashLinePaint.setStyle(Paint.Style.STROKE);
        float dashLineWidth = context.getResources().getDimensionPixelSize(R.dimen.oem_eraser_circle_default_stroke_width);
        eraserCircleDashLinePaint.setStrokeWidth(dashLineWidth);
        float factor = DisplayDpiHelper.getInstance().getFake4kFactor();
        eraserCircleDashLinePaint.setPathEffect(new DashPathEffect(new float[] {4f * factor, 4f * factor}, -1f * factor));
        eraserCircleDashLinePaint.setAntiAlias(true);
    }

    public void setRadius(float radius) {
        if (this.radius != radius) {
            this.radius = radius;
//            path.reset();
//            path.addCircle(verticalOffset, horizontalOffset, this.radius, Path.Direction.CW);
        }
    }

    public float getRadius() {
        return radius;
    }

    public void updatePosition(float x, float y) {
        setX(x-verticalOffset);
        setY(y-horizontalOffset);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(eraserInput == Whiteboard.Input.PARTIAL_ERASER){
//            paint.setAntiAlias(true);
//            paint.setStyle(Paint.Style.STROKE);
//            paint.setStrokeCap(Paint.Cap.ROUND);
//            paint.setStrokeJoin(Paint.Join.ROUND);
//            paint.setStrokeWidth(2);
//            paint.setPathEffect(dashPathEffect);
//            paint.setColor(strokeColor);
//            canvas.drawPath(path, paint);
//            paint.setPathEffect(null);
//            paint.setStyle(Paint.Style.FILL);
//            paint.setColor(fillColor);
//            canvas.drawCircle(verticalOffset, horizontalOffset, radius, paint);
            canvas.drawCircle(verticalOffset, horizontalOffset, radius, eraserCircleDashLinePaint);
        }
    }

    public void setEraserInput(Whiteboard.Input input){
        eraserInput = input;
//        if(eraserInput == Whiteboard.Input.PARTIAL_ERASER){
//            setBackgroundColor(ContextCompat.getColor(context, R.color.transparent));
//        }else{
//            setBackground(context.getDrawable(R.drawable.ic_ic_eraser_track));
//        }
        setBackgroundColor(ContextCompat.getColor(context, R.color.transparent));
    }

    private void log(String s) {
        Log.d(TAG, s);
    }
}
